package org.japo.java.matisse.samples;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

public class TipografiaLocal extends javax.swing.JFrame {

    // Referencia a la fuente
    private Font fuente;

    // Referencia a la imagen de fondo del panel
    private Image img;

    // Recurso con la imagen de fondo del panel
    private final String PKG_IMG = "/org/japo/java/resources/img";
    private final String RES_IMG = "explosion2.gif";
    private final String URL_IMG = PKG_IMG + "/" + RES_IMG;

    // Parámetros de la fuente
    private final String TIPOGRAFIA = "Go 2 Old Western";
    private final int ESTILO = Font.BOLD;
    private final int TALLA = 80;

    // Recurso con la fuente
    private final String PKG_FNT = "/org/japo/java/resources/fonts";
    private final String RES_FNT = "Go 2 Old Western.ttf";
    private final String URL_FNT = PKG_FNT + "/" + RES_FNT;

    public TipografiaLocal() {
        iniciarAnterior();
        initComponents();
        iniciarPosterior();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlFondo = new org.japo.java.swing.beans.PanelFondo();
        lblRotulo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tipografia");
        setUndecorated(true);

        pnlFondo.setImg(img);

        lblRotulo.setFont(fuente);
        lblRotulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblRotulo.setText("Golden Rush");
        lblRotulo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                lblRotuloMousePressed(evt);
            }
        });

        javax.swing.GroupLayout pnlFondoLayout = new javax.swing.GroupLayout(pnlFondo);
        pnlFondo.setLayout(pnlFondoLayout);
        pnlFondoLayout.setHorizontalGroup(
            pnlFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFondoLayout.createSequentialGroup()
                .addComponent(lblRotulo, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        pnlFondoLayout.setVerticalGroup(
            pnlFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblRotulo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlFondo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void lblRotuloMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblRotuloMousePressed
        terminarPrograma();
    }//GEN-LAST:event_lblRotuloMousePressed

    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TipografiaLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TipografiaLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TipografiaLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TipografiaLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TipografiaLocal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblRotulo;
    private org.japo.java.swing.beans.PanelFondo pnlFondo;
    // End of variables declaration//GEN-END:variables

    private void iniciarAnterior() {
        iniciarImagen();
        iniciarFuente();
    }

    private void iniciarPosterior() {
    }

    private void iniciarFuente() {
        // Registrar la fuente en el sistema
        registrarFuente();

        // Establecer fuente
        fuente = new Font(TIPOGRAFIA, ESTILO, TALLA);
    }

    private void registrarFuente() {
        try {
            // Empaqueta la dirección de la fuente
            InputStream is = getClass().getResourceAsStream(URL_FNT);

            // Instancia el objeto Font a partir de la URL
            Font f = Font.createFont(Font.TRUETYPE_FONT, is);

            // Obtiene la referencia al sistema gráfico
            GraphicsEnvironment ge
                    = GraphicsEnvironment.getLocalGraphicsEnvironment();

            // Registra la fuente en el sistema gráfico
            ge.registerFont(f);
        } catch (FontFormatException | IOException ex) {
            Logger.getLogger(TipografiaLocal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void iniciarImagen() {
        // Empaqueta la dirección de la imagen de fondo
        URL url = getClass().getResource(URL_IMG);

        // Instancia el objeto Image a partir de la URL
        img = new ImageIcon(url).getImage();
    }

    private void terminarPrograma() {
        // Oculta la ventana
        setVisible(false);
        
        // Libera recursos
        dispose();
        
        // Termina el programa
        System.exit(0);
    }
}
